package com.demo.connectdbtest.datasourceconfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 描述：
 *
 * @author: chengc
 * @date 2021/03/31
 */

@Configuration
@MapperScan(basePackages = "com.demo.connectdbtest.module.*.meMapper", sqlSessionTemplateRef="masterSqlSessionTemplate")
public class ConfigurationDatasource {
    private static final Logger log = LoggerFactory.getLogger(ConfigurationDatasource.class);
//    spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
//    spring.datasource.master.username=root
//    spring.datasource.master.url=jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8
//    spring.datasource.master.password=123456
    @Value("${spring.datasource.master.url}")
    private String url;
    @Value("${spring.datasource.master.password}")
    private String password;
    @Value("${spring.datasource.master.username}")
    private String username;
    @Value("${spring.datasource.master.driver-class-name}")
    private String driverClassName;

    @Bean(name = "masterDataSource")
   // @ConfigurationProperties("spring.datasource.master")
    public DataSource masterDataSource(){
        log.info("url={},pwd={},username={},driver={}",url,password,username,driverClassName);
       return DataSourceBuilder.create().url(url).username(username).password(password).driverClassName(driverClassName).build();
       // return DataSourceBuilder.create().build();
    }

    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/meMapper" +
                "/*.xml");
        sessionFactoryBean.setMapperLocations(resources);
        return sessionFactoryBean.getObject();
    }
    @Bean(name = "masterTransactionManager")
    public PlatformTransactionManager testTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "masterSqlSessionTemplate")
    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
